package com.scu.hotelsystem.controller;

import com.scu.hotelsystem.pojo.RoomBill;
import com.scu.hotelsystem.service.RoomBillService;
import com.scu.hotelsystem.util.DateConverter;
import com.scu.hotelsystem.util.PageInfo;
import com.scu.hotelsystem.util.Result;
import com.scu.hotelsystem.util.search.RoomBillSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class RoomBillController {
    @Autowired
    private RoomBillService roomBillService;

    @RequestMapping("/roomBillPage")
    public String roomBillPage(){
        return "roomBillPage";
    }

    @RequestMapping("/addRoomBill")
    public String addRoomBill(){
        return "roomBill-add";
    }

    @RequestMapping("/addRoom_Bill")
    @ResponseBody
    public Result addRoom_Bill(int guest_id_1, int guest_id_2, int room_id, int staff_id, int last_days, String start_time, double total){
        DateConverter dateConverter=new DateConverter();
        Date start=dateConverter.convert(start_time);

        RoomBill roomBill=new RoomBill(guest_id_1,guest_id_2,staff_id,room_id,last_days,start,1,total,1);
        int n;
        System.out.println(roomBill);
        n=roomBillService.addRoomBill(roomBill);

        if (n == 1){
            return new Result();
        }else {
            return new Result(1, "未知错误");
        }
    }

    @RequestMapping("/searchRoomBillList")
    @ResponseBody
     public Result searchRoomBillList(RoomBillSearch roomBillSearch, PageInfo pageInfo){
        List<RoomBill> roomBillList;
        int count;
        System.out.println(roomBillSearch.getAdd_time());
        System.out.println(roomBillSearch.getGuest_id());
        System.out.println(roomBillSearch.getRoom_id());
        roomBillList= roomBillService.searchRoomBillList(roomBillSearch, pageInfo.index(), pageInfo.limit());

        count = roomBillList.size();
        System.out.println(count);
        return new Result(count, roomBillList);
    }
   /* public Result searchRoomBillList(Integer guest_id,Integer room_id,Integer staff_id,String add_time , PageInfo pageInfo){
        List<RoomBill> roomBillList;
        int count;
        DateConverter dateConverter=new DateConverter();
        Date add=dateConverter.convert(add_time);
        RoomBillSearch roomBillSearch=new RoomBillSearch(guest_id,room_id,staff_id,add);
        System.out.println(roomBillSearch.getAdd_time());
        roomBillList= roomBillService.searchRoomBillList(roomBillSearch, pageInfo.index(), pageInfo.limit());
        count = roomBillList.size();
        System.out.println(count);
        return new Result(count, roomBillList);
    }*/



    @RequestMapping("/deleteRoomBill")
    @ResponseBody
    public Result deleteRoomBusiness(int[] idList){
        if (idList.length == 0){
            return new Result(2, "列表为空");
        }

        int n;
        try{
            n = roomBillService.deleteRoomBill(idList);
        }catch (IllegalArgumentException e){
            n = 0;
        }

        if (n == 0){
            return new Result(1, "失败");
        }else {
            return new Result();
        }

    }


}
